import { toStringTypeCheck } from '../../../../global/global';

//form Comps  --Src
let rootSrc = '../formTable/';

let FormTableCompArr = ['fileApi', 'fileType'];

let formDetectData = {
  data: ['inputcheck'],
  inputcheck: { target: 'el', checkfunc: testA },
};

let AddCompsData = { data: [] };

function testA() {
  console.log('hha');
  return true;
}

function formDetect(detectData) {
  let result = { data: [], result: false };
  for (let i = 0; i < detectData['data'].length; i++) {
    let singleResult = detectData[detectData['data'][i]]['checkfunc']();

    result['data'].push(detectData['data'][i]);
    result[detectData['data'][i]] = singleResult;

    result['result'] == false
      ? singleResult == true
        ? (result['result'] = true)
        : undefined
      : undefined;
  }

  return result;
}

function assembleFormDetect(name, el, func, err) {
  let errorlogqn = false;

  if (name != undefined ? (typeof name == 'string' ? true : false) : false) {
    if (name == 'all') {
      console.log(formDetectData['data']);
      return formDetectData['data'];
    }
    // 参数已存在的情况下
    if (formDetectData['data'].indexOf(name) > -1) {
      errorlogqn = true;
      console.log(
        "this name is already assigned, you can change it or look name arr use assembleFormDetect('all')"
      );
      return;
    }

    formDetectData['data'].push(name);
    formDetectData[name] = {};

    if (el != undefined ? (typeof el == 'string' ? true : false) : false) {
      formDetectData[name]['target'] = el;
    } else {
      errorlogqn = true;

      console.log(
        "did you forget assign 'targrt' Element at 1st params, we need Element ID<String> to parse Error commend"
      );
    }

    if (
      func != undefined ? (typeof func == 'function' ? true : false) : false
    ) {
      formDetectData[name]['checkfunc'] = func;
    } else {
      errorlogqn = true;

      console.log(
        "did you forget assign 'checkfunc' at 2nd params, we need function<function> to rescue correct value in current part"
      );
    }

    if (err != undefined ? (typeof err == 'string' ? true : false) : false) {
      formDetectData[name]['error'] = err;
    } else {
      errorlogqn = true;

      console.log(
        "did you forget assign 'error' at 3rd params, we need error<string> to parse Error commend for rescue correct value in current part"
      );
    }
  } else {
    console.log('your Name is undefined is not allowed to assign');
  }

  if (errorlogqn) {
    if (formDetectData['data'].indexOf(name) > -1) {
      //   回滚数据
      console.log('reback the formDetectData -- 回滚数据');
      formDetectData['data'].splice(formDetectData['data'].indexOf(name), 1);
      formDetectData[name] = undefined;
    }
  }

  return formDetectData;
}

//没写完 需要进行 import 进行动态添加 组件
async function importFormTable(comps) {
  if (toStringTypeCheck(comps, 'array') && comps.length > 0) {
    importFormTable(comps[0]);
    comps.splice(0, 1);
    // console.log(comps);
    AddCompsData['once'] = comps.length;
    importFormTable(comps);
  }
  if (AddCompsData['once'] === false) {
    AddCompsData['once'] = true;

    // console.log(AddCompsData);
    // run importFunc
  }
  if (AddCompsData['once'] === 0) {
    AddCompsData['once'] = false;
  }

  if (toStringTypeCheck(comps, 'string')) {
    if (!checkFormTableComps(comps)) {
      console.log(
        `we got the inexistent Component '${comps}' over the FormTable Comps`
      );
      return;
    }
    AddCompsData['data'].push(comps);
    let joinCompStr = rootSrc + comps;
    console.log(joinCompStr);
    // let a = await import(joinCompStr);
    // AddCompsData[comps] = a;
  }
}

function checkFormTableComps(comps) {
  return FormTableCompArr.indexOf(comps) > -1;
}

/********************only for test************************/

function avandaew() {
  let a = formDetect(formDetectData);
  console.log(a);
}
export { avandaew, assembleFormDetect, importFormTable };
